import 'package:flutter/material.dart';
import 'package:flutter_taotaoshipin/common/theme/dimens.dart';
import 'package:flutter_taotaoshipin/model/drama_banner_entity.dart';
import 'package:flutter_taotaoshipin/pages/drama/drama_routes.dart';
import 'package:flutter_taotaoshipin/widget/load_image.dart';
import 'package:flutter_swiper_null_safety_flutter3/flutter_swiper_null_safety_flutter3.dart';
import 'package:get/get.dart';

class IndexBanner extends StatelessWidget {
  final List<DramaBannerEntity> bannerList;
  final double aspectRatio;
  final EdgeInsetsGeometry? padding;
  final ValueChanged<int>? onIndexChanged;

  const IndexBanner(
      {super.key,
      required this.bannerList,
      this.aspectRatio = 2,
      this.padding,
      this.onIndexChanged});

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: aspectRatio,
      child: _banner(),
    );
  }

  _banner() {
    double right = 10 + (padding?.horizontal ?? 0) / 2;
    return Swiper(
      itemCount: bannerList.length,
      autoplay: true,
      viewportFraction: 0.6,
      scale: 0.8,
      itemBuilder: (context, index) {
        return _image(bannerList[index]);
      },
      onIndexChanged: onIndexChanged,
      // 自定义指示器
      pagination: null,
    );
  }

  _image(DramaBannerEntity banner) {
    return InkWell(
      onTap: () {
        Get.toNamed(
          DramaRoutes.detail,
          arguments: {
            'id': banner.dramaId,
            'title': banner.dramaName,
          },
        );
      },
      child: Container(
        padding: padding,
        child: Stack(
          children: [
            Positioned.fill(
              child: ClipRRect(
                borderRadius: const BorderRadius.all(Radius.circular(8)),
                child: LoadImage(
                  banner.bannerImg ?? '',
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
